/*
 * @Author: ZhangXiaodong
 * @Date: 2025-04-12 10:18:03
 * @LastEditors: ZhangXiaodong
 * @LastEditTime: 2025-04-25 23:24:55
 * @Description: blog路由
 * @FilePath: /blog-ost/src/routers/blog/index.js
 */
const { SuccessModel, ErrorModel } = require("../../model/ResModel");
const ctrlBlog = require("../../controller/blog");
const { json } = require("stream/consumers");
const routerBlog = (req) => {
  const { path, method } = req;
  // 博客列表
  if (method === "GET" && path == "/api/blog/list") {
    const blogListResult = ctrlBlog.getList({
      author: req.query.author || "",
      keyword: req.query.keyword || "",
    });

    return blogListResult.then((result) => {
      return new SuccessModel(result, "获取成功");
    });
  }
  // 博客详情
  if (method === "GET" && path == "/api/blog/detail") {
    if (!req.query.id) {
      return null;
    }
    const blogDetailResult = ctrlBlog.getDetail(req.query.id);
    return blogDetailResult.then((result) => {
      return new SuccessModel(result, "获取成功");
    });
  }
  // 博客创建
  if (method === "POST" && path == "/api/blog/create") {
    if (!req.body.title || !req.body.content || !req.body.author) {
      return Promise.resolve(new ErrorModel("参数错误"));
    }
    const blogCreateResult = ctrlBlog.createBlog(req.body);
    return blogCreateResult.then((result) => {
      return new SuccessModel(result, "创建成功");
    });
  }
  // 博客修改
  if (method === "POST" && path == "/api/blog/set") {
    const blogSetResult = ctrlBlog.setBlog(req.id, req.body);
    return blogSetResult.then((result) => {
      if (!result) {
        return new ErrorModel("修改失败");
      }
      return new SuccessModel("修改成功");
    });
  }
  // 博客删除
  if (method === "POST" && path == "/api/blog/delete") {
    
    const blogDeleteResult = ctrlBlog.deleteBlog(req.id,req.userId);
    return blogDeleteResult.then((result) => {
      if(!result){
        return new ErrorModel("删除失败");
      }
        return new SuccessModel("删除成功");
      
      
    });
  }
};

module.exports = routerBlog;
